import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/login'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component:Login,
    meta:{
      isNav:true
    }
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// router.beforeEach((to,from,next)=>{
//   var auth = true
//   if(to.path=='/login'){
//     if(auth){
//       router.push('/home')
//     }else{
//       next();
//     }
//   }else{
//     // 不在登录页
//     if(auth){
//       next()
//     }else{
//       router.push('/login')
//     }
//   }
// })
var vm = new Vue()
router.beforeEach((to,from,next)=>{
  var auth = vm.$cookie.get('loginAuth')
  if(to.path=='/login'){
    if(auth){
      router.push('/home')
    }else{
      next()
    }
  }else{
    if(auth){
      next()
    }else{
      router.push('/login')
    }
  }
})
export default router
